// 
// Copyright(c) 2009 Syntext, Inc. All Rights Reserved.
// Contact: info@syntext.com, http://www.syntext.com
// 
// This file is part of Syntext Serna XML Editor.
// 
// COMMERCIAL USAGE
// Licensees holding valid Syntext Serna commercial licenses may use this file
// in accordance with the Syntext Serna Commercial License Agreement provided
// with the software, or, alternatively, in accorance with the terms contained
// in a written agreement between you and Syntext, Inc.
// 
// GNU GENERAL PUBLIC LICENSE USAGE
// Alternatively, this file may be used under the terms of the GNU General 
// Public License versions 2.0 or 3.0 as published by the Free Software 
// Foundation and appearing in the file LICENSE.GPL included in the packaging 
// of this file. In addition, as a special exception, Syntext, Inc. gives you
// certain additional rights, which are described in the Syntext, Inc. GPL 
// Exception for Syntext Serna Free Edition, included in the file 
// GPL_EXCEPTION.txt in this package.
// 
// You should have received a copy of appropriate licenses along with this 
// package. If not, see <http://www.syntext.com/legal/>. If you are unsure
// which license is appropriate for your use, please contact the sales 
// department at sales@syntext.com.
// 
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// 
// Copyright (c) 2004 Syntext Inc.
//
// This is a copyrighted commercial software.
// Please see COPYRIGHT file for details.

/** \file
 */

#include "utils/TimedEventGenerator.h"
#include "utils/utils_debug.h"

#include "common/CommandEvent.h"

#include <QObject>

#ifdef DBG_DEFAULT_TAG
# undef DBG_DEFAULT_TAG
#endif
#define DBG_DEFAULT_TAG UTILS.TIMERS

class QTimerEvent;

class QtTimedEventGenerator : public TimedEventGenerator, private QObject {
public:
    QtTimedEventGenerator();
    ~QtTimedEventGenerator();

    virtual void timerEvent(QTimerEvent*);

    virtual void subscribe(Common::EventMakerPtr eventMakerFunc,
                           Common::CommandEventContext* subscriber);

    virtual void unsubscribe(Common::CommandEventContext* subscriber);

    virtual void reset(int timeout);
private:
    void stop();
    Common::DynamicEventFactory evf_;
    int timerId_;
};

QtTimedEventGenerator::QtTimedEventGenerator()
 :  timerId_(-1)
{
    DDBG << "QtTimedEventGenerator(), this = " << abr(this) << std::endl;
}

QtTimedEventGenerator::~QtTimedEventGenerator()
{
    DDBG << "~QtTimedEventGenerator(), this = " << abr(this) << std::endl;
}

void QtTimedEventGenerator::subscribe(Common::EventMakerPtr eventMakerFunc,
                                      Common::CommandEventContext* subscriber)
{
    DDBG << "QtTimedEventGenerator(), subscribed " << abr(subscriber)
         << std::endl;
    evf_.subscribe(eventMakerFunc, subscriber);
}

void QtTimedEventGenerator::unsubscribe(Common::CommandEventContext* subscriber)
{
    DDBG << "QtTimedEventGenerator(), unsubscribed " << abr(subscriber)
         << std::endl;
    evf_.unsubscribe(subscriber);
}

void QtTimedEventGenerator::stop()
{
    if (-1 != timerId_) {
        killTimer(timerId_);
        timerId_ = -1;
    }
}

void QtTimedEventGenerator::reset(int timeout)
{
    DDBG << "QtTimedEventGenerator(), reset timer to " << timeout << std::endl;
    stop();
    if (0 < timeout)
        timerId_ = startTimer(timeout);
}

void QtTimedEventGenerator::timerEvent(QTimerEvent*)
{
    DDBG << "QtTimedEventGenerator(), timerEvent arrived " << this << std::endl;
    evf_.dispatchEvent();
    stop();
}

TimedEventGenerator::TimedEventGenerator() {}
TimedEventGenerator::~TimedEventGenerator() {}

TimedEventGenerator* TimedEventGenerator::make()
{
    return new QtTimedEventGenerator;
}

